<template>
  <a-row :gutter="10">
    <a-col>
      <div style="background: #fff;height: 100%; margin-top: 5px">
        <a-input-search @search="onSearch" style="width:100%;margin-top: 10px" size="small" placeholder="请输入机构名称" />
        <!-- 树-->
        <template v-if="userIdentity === '2' && departTree.length > 0">
          <!--组织机构-->
          <a-tree showLine :selectedKeys="selectedKeys" v-model="checkedKeys" :checkStrictly="true" @select="onSelect"
            :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }" :treeData="departTree"
            :autoExpandParent="autoExpandParent" :expandedKeys="iExpandedKeys" @expand="onExpand"
            style="font-size: 12px;" />
        </template>
        <div style="margin-top: 24px;" v-else-if="userIdentity === '2' && departTree.length == 0">
          <h3><span>您的部门下暂无有效部门信息</span></h3>
        </div>
        <div style="margin-top: 24px;" v-else>
          <h3>普通员工暂无此权限</h3>
        </div>
      </div>
    </a-col>
  </a-row>
</template>
<script>
import { queryMyDepartTreeList, searchByKeywords } from '@/api/api'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import store from '@/store'
import { Text } from 'vue-json-editor/assets/jsoneditor'

export default {
  name: 'DeviceOrganizeTree',
  props: {
    expandedkeys: {
      type: Array,
    },
  },
  mixins: [JeecgListMixin],
  components: {
    Text
  },
  data() {
    return {
      currentDeptId: '',
      iExpandedKeys: [],
      loading: false,
      autoExpandParent: true,
      currFlowId: '',
      currFlowName: '',
      disable: true,
      treeData: [],
      visible: false,
      departTree: [],
      rightClickSelectedKey: '',
      hiding: true,
      model: {},
      dropTrigger: '',
      depart: {},
      disableSubmit: false,
      checkedKeys: [],
      selectedKeys: [],
      autoIncr: 1,
      currSelected: {},
      curSelectedOrgCode: '',
      form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 2 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      },
      graphDatasource: {
        nodes: [],
        edges: []
      },
      userIdentity: "",
      bUserAdmin: false,
    }
  },

  // created(){
  //   this.iExpandedKeys = this.expandedkeys
  // },
  methods: {
    beforeCreate() {
      this.iExpandedKeys = this.expandedkeys
    },
    callback(key) {
      //console.log(key)
    },
    loadData() {
      this.refresh();
    },
    getSelectOrgCode() {
      return this.curSelectedOrgCode;
    },
    setSelectOrgCode(data) {
      this.selectedKeys.push(data.orgId)
      this.curSelectedOrgCode = data.orgCode
    },
    clearSelectedDepartKeys() {
      this.checkedKeys = [];
      this.selectedKeys = [];
      this.currentDeptId = '';
    },
    loadTree() {
      var that = this
      that.treeData = []
      that.departTree = []
      queryMyDepartTreeList().then((res) => {
        if (res.success && res.result) {
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i]
            that.treeData.push(temp)
            that.departTree.push(temp)
            that.setThisExpandedKeys(temp)
            //that.curSelectedOrgCode = temp.orgCode
            //this.$emit('changeSelect', temp.orgCode);
            // console.log(temp.id)
          }
          this.loading = false
        }
        that.userIdentity = res.message
        if (this.selectedKeys.length == 0) {
          let id = this.$store.getters.userInfo.departIds
          this.selectedKeys.push(id)
        }
      })
    },
    setThisExpandedKeys(node) {
      //只展开一级目录
      if (node.children && node.children.length > 0) {
        this.iExpandedKeys.push(node.key)
        //下方代码放开注释则默认展开所有节点
        /**
        for (let a = 0; a < node.children.length; a++) {
          this.setThisExpandedKeys(node.children[a])
        }
        */
      }
    },
    refresh() {
      this.loading = true
      this.loadTree()
    },

    onExpand(expandedKeys) {
      // console.log('onExpand', expandedKeys)
      // if not set autoExpandParent to false, if children expanded, parent can not collapse.
      // or, you can remove all expanded children keys.
      this.iExpandedKeys = expandedKeys
      this.autoExpandParent = false
      this.$emit('changeExpand', expandedKeys);
    },

    onSearch(value) {
      let that = this
      if (value) {
        searchByKeywords({ keyWord: value, myDeptSearch: '1' }).then((res) => {
          if (res.success) {
            that.departTree = []
            for (let i = 0; i < res.result.length; i++) {
              let temp = res.result[i]
              that.departTree.push(temp)
            }
          } else {
            that.$message.warning(res.message)
          }
        })
      } else {
        that.loadTree()
      }

    },
    onCheck(checkedKeys, e) {
      let record = e.node.dataRef;
      // console.log('onCheck', checkedKeys, e);
      this.checkedKeys = [];
      // if (e.checked === true) {
      this.currentDeptId = record.id;
      this.checkedKeys.push(record.id);

      this.hiding = false;
      // this.checkedKeys = checkedKeys.checked
    },
    onSelect(selectedKeys, e) {
      if (this.selectedKeys[0] !== selectedKeys[0]) {
        this.selectedKeys = [selectedKeys[0]];
      }
      let record = e.node.dataRef;
      this.checkedKeys.push(record.id);
      this.curSelectedOrgCode = record.orgCode;
      this.$emit('changeSelect', {id:record.id,orgCode:record.orgCode});
    },
  },
  created() {
    this.currFlowId = this.$route.params.id
    this.currFlowName = this.$route.params.name
  },

}
</script>
<style scoped>
@import '~@assets/less/common.less'
</style>